為了確保系統會自動釋放完整資源,呼叫時使用 using 來呼叫對應的方法處理封包,而結束時垃圾收集行程(GC)並不會處置該物件,需呼叫 Dispose 的方法來手動釋放資源,包括Buffer暫存及讀取位置。
private bool disposed = false;
protected virtual void Dispose(bool _disposing)
{
    if (!disposed)
    {
        if (_disposing)
        {
            buffer = null;
            readableBuffer = null;
            readPos = 0;
        }
        disposed = true;
    }
}
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}
為了提高系統的效能,封包的處理以及優化是很重要的。